package visao.cliente;

import visao.usuario.InterfaceLogin;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import modelo.dominio.Cliente;
import modelo.persistencia.ControleAcesso;
import modelo.persistencia.PersistenciaCliente;

/*
 * Está classe configura a Interface Pesquisar Cliente no Banco de Dados
 */
public class InterfacePesquisarCliente extends javax.swing.JFrame{
	
	private final PersistenciaCliente persistenciaCliente = new PersistenciaCliente();
	
	/*
	 * Construtor da classe Interface Pesquisar Cliente
	 */
	public InterfacePesquisarCliente() {
		initComponents(); 
		
		this.setTitle("Pesquisar Cliente");
		
		jLabel1.setText("CPF :");
		jTextField1.setText("");
		
	}// Fim do Construtor
	
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
			  
            },
            new String [] {
                "Nome", "CPF", "Telefone", "Endereço" 
            }
        ){
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Long.class, java.lang.String.class, java.lang.String.class
            };
            
            
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        DefaultTableCellRenderer esquerda = new DefaultTableCellRenderer();  
        DefaultTableCellRenderer centralizado = new DefaultTableCellRenderer();  
        esquerda.setHorizontalAlignment(SwingConstants.LEFT);  
        centralizado.setHorizontalAlignment(SwingConstants.CENTER);  
        jTable1.getColumnModel().getColumn(0).setCellRenderer(esquerda);  
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(110);
        jTable1.getColumnModel().getColumn(1).setCellRenderer(esquerda); 
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(40);
        jTable1.getColumnModel().getColumn(2).setCellRenderer(esquerda); 
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(20);
        jTable1.getColumnModel().getColumn(3).setCellRenderer(esquerda); 
        jTable1.getColumnModel().getColumn(3).setPreferredWidth(110);
	   
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jButton1.setText("Logoff");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLogoff(evt);
            }
        });

        jButton2.setText("Voltar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVoltar(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel1.setText("jLabel1");

        jButton3.setText("Ok");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					jButtonOkActionPerformed(evt);
				} catch (IOException ex) {
					Logger.getLogger(InterfacePesquisarCliente.class.getName()).log(Level.SEVERE, null, ex);
				}
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 587, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addGap(31, 31, 31)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>

	/*
	 * Método do Botão Voltar
	 */
	private void jButtonVoltar(java.awt.event.ActionEvent evt) {                               
		dispose();
		ControleAcesso.voltarMenuPrincipal();
	}// Fim do Método voltar                              

	/*
	 * Método do Botão Log Off que retornar a Interface de Login
	 */
	private void jButtonLogoff(java.awt.event.ActionEvent evt) {                               
		dispose();
		ControleAcesso.logoff();
	}                              
	// Fim do método log off

	private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
		jTable1.setModel(new javax.swing.table.DefaultTableModel(
			new Object [][] {
			  
			},
			new String [] {
				"Nome", "CPF", "Telefone", "Enredeço"
			}
		));
		String cpfPes = jTextField1.getText();
		long cpf = Long.parseLong(cpfPes);
		Cliente cliente = new Cliente(cpf);
		Cliente novoCliente = (Cliente) persistenciaCliente.consultar(cliente);
		if(!cpfPes.equals("")){
			if(novoCliente != null){			
				String nome, telefone, endereco;
				long  cpf2;
				nome = novoCliente.getNome();
				cpf2 = novoCliente.getCpf();
				telefone = novoCliente.getTelefone();
				endereco = novoCliente.getEndereco();
				((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{nome,cpf2,telefone,endereco});
		
				jLabel1.setText("CPF :");
				jTextField1.setText("");
			}else {
				JOptionPane.showMessageDialog(null,"<html> <font color='black'>Nenhum Cliente com esse CPF.</font> </html>","Erro",JOptionPane.ERROR_MESSAGE);
				jLabel1.setText("CPF :");
				jTextField1.setText("");
			}
		}else {
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Digite um CPF por favor!</font> </html>","Erro",JOptionPane.ERROR_MESSAGE);
		}		
	}
	// Fim do método Confirmar
	
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
}// Fim da classe interface pesquisar cliente 
